/* Copyright (C) 2005-2013, Unigine Corp. All rights reserved.
 *
 * File:    fragment_terrain_sample.h
 * Desc:    Decal terrain sample shader
 * Version: 1.01
 * Author:  Alexander Zapryagaev <frustum@unigine.com>
 *
 * This file is part of the Unigine engine (http://unigine.com/).
 *
 * Your use and or redistribution of this software in source and / or
 * binary form, with or without modification, is subject to: (i) your
 * ongoing acceptance of and compliance with the terms and conditions of
 * the Unigine License Agreement; and (ii) your inclusion of this notice
 * in any version of this software that you use or redistribute.
 * A copy of the Unigine License Agreement is available by contacting
 * Unigine Corp. at http://unigine.com/
 */

/******************************************************************************\
*
* OpenGL
*
\******************************************************************************/

#ifdef OPENGL
	
	half fade = s_texcoord_0.z;
	
	float2 texcoord = s_texcoord_0.xy / s_texcoord_0.w;
	if(any(greaterThan(abs(texcoord.xy - 0.5f),half2(0.5f)))) discard;
	
	#ifdef BASE_DEFERRED
		half4 diffuse = texture2DAlphaDiscard(s_texture_0,texcoord,fade);
	#else
		half4 diffuse = texture2DAlpha(s_texture_0,texcoord,fade);
	#endif
	
	half alpha = diffuse.w;
	
	#ifdef DETAIL_0_OVERLAY || DETAIL_0_OVERLAP || DETAIL_1_OVERLAY || DETAIL_1_OVERLAP || DETAIL_2_OVERLAY || DETAIL_2_OVERLAP || DETAIL_3_OVERLAY || DETAIL_3_OVERLAP
		
		half3 normal = half3(0.0f);
		half3 basis_normal = texture2DNormal(s_texture_1,texcoord);
		half3 basis_binormal = normalize(cross(half3(-1.0f,0.0f,0.0f),basis_normal));
		half3 basis_tangent = normalize(cross(basis_binormal,basis_normal));
		
		half4 mask = texture(s_texture_2,texcoord);
		
		#ifdef QUALITY_MEDIUM
			#define DETAIL_OVERLAY(DIFFUSE,NORMAL,NUM,SWIZZLE) { \
				float2 detail_texcoord = texcoord * detail_ ## NUM ## _transform.xy + detail_ ## NUM ## _transform.zw; \
				half4 detail_diffuse = texture(s_texture_ ## DIFFUSE,detail_texcoord); \
				half3 detail_normal = texture2DNormal(s_texture_ ## NORMAL,detail_texcoord); \
				diffuse = saturate(diffuse + (detail_diffuse * 2.0f - 1.0f) * (detail_ ## NUM ## _diffuse * mask. SWIZZLE)); \
				normal = normal + detail_normal * (detail_ ## NUM ## _normal * mask. SWIZZLE); \
			}
		#else
			#define DETAIL_OVERLAY(DIFFUSE,NORMAL,NUM,SWIZZLE) { \
				float2 detail_texcoord = texcoord * detail_ ## NUM ## _transform.xy + detail_ ## NUM ## _transform.zw; \
				half4 detail_diffuse = texture(s_texture_ ## DIFFUSE,detail_texcoord); \
				diffuse = saturate(diffuse + (detail_diffuse * 2.0f - 1.0f) * (detail_ ## NUM ## _diffuse * mask. SWIZZLE)); \
			}
		#endif
		
		#ifdef QUALITY_MEDIUM
			#define DETAIL_OVERLAP(DIFFUSE,NORMAL,NUM,SWIZZLE) { \
				float2 detail_texcoord = texcoord * detail_ ## NUM ## _transform.xy + detail_ ## NUM ## _transform.zw; \
				half4 detail_diffuse = texture(s_texture_ ## DIFFUSE,detail_texcoord); \
				half3 detail_normal = texture2DNormal(s_texture_ ## NORMAL,detail_texcoord); \
				half detail_alpha = saturate((detail_diffuse.w * mask. ## SWIZZLE - detail_ ## NUM ## _value + detail_ ## NUM ## _threshold * 0.5f) / max(detail_ ## NUM ## _threshold * 0.5f,1e-6f)); \
				diffuse.xyz = lerp(diffuse.xyz,detail_diffuse.xyz,detail_alpha * detail_ ## NUM ## _diffuse); \
				normal.xy = lerp(normal.xy,detail_normal.xy,detail_alpha * detail_ ## NUM ## _normal); \
			}
		#else
			#define DETAIL_OVERLAP(DIFFUSE,NORMAL,NUM,SWIZZLE) { \
				float2 detail_texcoord = texcoord * detail_ ## NUM ## _transform.xy + detail_ ## NUM ## _transform.zw; \
				half4 detail_diffuse = texture(s_texture_ ## DIFFUSE,detail_texcoord); \
				half detail_alpha = saturate((detail_diffuse.w * mask. ## SWIZZLE - detail_ ## NUM ## _value + detail_ ## NUM ## _threshold * 0.5f) / max(detail_ ## NUM ## _threshold * 0.5f,1e-6f)); \
				diffuse.xyz = lerp(diffuse.xyz,detail_diffuse.xyz,detail_alpha * detail_ ## NUM ## _diffuse); \
			}
		#endif
		
		#ifdef DETAIL_0_OVERLAY
			DETAIL_OVERLAY(3,4,0,x);
		#elif DETAIL_0_OVERLAP
			DETAIL_OVERLAP(3,4,0,x);
		#endif
		#ifdef DETAIL_1_OVERLAY
			DETAIL_OVERLAY(5,6,1,y);
		#elif DETAIL_1_OVERLAP
			DETAIL_OVERLAP(5,6,1,y);
		#endif
		#ifdef DETAIL_2_OVERLAY
			DETAIL_OVERLAY(7,8,2,z);
		#elif DETAIL_2_OVERLAP
			DETAIL_OVERLAP(7,8,2,z);
		#endif
		#ifdef DETAIL_3_OVERLAY
			DETAIL_OVERLAY(9,10,3,w);
		#elif DETAIL_3_OVERLAP
			DETAIL_OVERLAP(9,10,3,w);
		#endif
		
		#undef DETAIL_OVERLAY
		#undef DETAIL_OVERLAP
		
		normal = normalize(basis_normal + basis_tangent * normal.x + basis_binormal * normal.y);
		
	#else
		
		half3 normal = texture2DNormal(s_texture_1,texcoord);
		
	#endif
	
	#ifdef USE_SRGB
		diffuse.xyz *= diffuse.xyz;
	#endif
	
	half3 specular = diffuse.xyz * diffuse.w;
	
	#ifdef ALPHA_TEST
		diffuse.w = saturate(alpha * 2.0f - 1.0f);
	#else
		diffuse.w = alpha;
	#endif
	
/******************************************************************************\
*
* Direct3D11
*
\******************************************************************************/

#elif DIRECT3D11
	
	half fade = IN.texcoord_0.z;
	
	float2 texcoord = IN.texcoord_0.xy / IN.texcoord_0.w;
	if(any(abs(texcoord.xy - 0.5f) > 0.5f)) discard;
	
	#ifdef BASE_DEFERRED
		FRAGMENT_ALPHA_OUT ALPHA_OUT;
		ALPHA_OUT = texture2DAlphaDiscard(s_texture_0,s_sampler_0,texcoord,fade);
		#ifdef ALPHA_TEST && !MULTISAMPLE_0 && USE_ALPHA_TEST_LEVEL_10_1
			OUT.coverage = ALPHA_OUT.coverage;
		#endif
		half4 diffuse = ALPHA_OUT.color;
	#else
		OUT = texture2DAlpha(s_texture_0,s_sampler_0,texcoord,fade);
		half4 diffuse = OUT.color;
	#endif
	
	half alpha = diffuse.w;
	
	#ifdef DETAIL_0_OVERLAY || DETAIL_0_OVERLAP || DETAIL_1_OVERLAY || DETAIL_1_OVERLAP || DETAIL_2_OVERLAY || DETAIL_2_OVERLAP || DETAIL_3_OVERLAY || DETAIL_3_OVERLAP
		
		half3 normal = 0.0f;
		half3 basis_normal = texture2DNormal(s_texture_1,s_sampler_1,texcoord);
		half3 basis_binormal = normalize(cross(half3(-1.0f,0.0f,0.0f),basis_normal));
		half3 basis_tangent = normalize(cross(basis_binormal,basis_normal));
		
		half4 mask = s_texture_2.Sample(s_sampler_2,texcoord);
		
		#ifdef QUALITY_MEDIUM
			#define DETAIL_OVERLAY(DIFFUSE,NORMAL,NUM,SWIZZLE) { \
				float2 detail_texcoord = texcoord * detail_ ## NUM ## _transform.xy + detail_ ## NUM ## _transform.zw; \
				half4 detail_diffuse = s_texture_ ## DIFFUSE .Sample(s_sampler_ ## DIFFUSE,detail_texcoord); \
				half3 detail_normal = texture2DNormal(s_texture_ ## NORMAL,s_sampler_ ## NORMAL,detail_texcoord); \
				diffuse = saturate(diffuse + (detail_diffuse * 2.0f - 1.0f) * (detail_ ## NUM ## _diffuse * mask. SWIZZLE)); \
				normal = normal + detail_normal * (detail_ ## NUM ## _normal * mask. SWIZZLE); \
			}
		#else
			#define DETAIL_OVERLAY(DIFFUSE,NORMAL,NUM,SWIZZLE) { \
				float2 detail_texcoord = texcoord * detail_ ## NUM ## _transform.xy + detail_ ## NUM ## _transform.zw; \
				half4 detail_diffuse = s_texture_ ## DIFFUSE .Sample(s_sampler_ ## DIFFUSE,detail_texcoord); \
				diffuse = saturate(diffuse + (detail_diffuse * 2.0f - 1.0f) * (detail_ ## NUM ## _diffuse * mask. SWIZZLE)); \
			}
		#endif
		
		#ifdef QUALITY_MEDIUM
			#define DETAIL_OVERLAP(DIFFUSE,NORMAL,NUM,SWIZZLE) { \
				float2 detail_texcoord = texcoord * detail_ ## NUM ## _transform.xy + detail_ ## NUM ## _transform.zw; \
				half4 detail_diffuse = s_texture_ ## DIFFUSE .Sample(s_sampler_ ## DIFFUSE,detail_texcoord); \
				half3 detail_normal = texture2DNormal(s_texture_ ## NORMAL,s_sampler_ ## NORMAL,detail_texcoord); \
				half detail_alpha = saturate((detail_diffuse.w * mask. ## SWIZZLE - detail_ ## NUM ## _value + detail_ ## NUM ## _threshold * 0.5f) / max(detail_ ## NUM ## _threshold * 0.5f,1e-6f)); \
				diffuse.xyz = lerp(diffuse.xyz,detail_diffuse.xyz,detail_alpha * detail_ ## NUM ## _diffuse); \
				normal.xy = lerp(normal.xy,detail_normal.xy,detail_alpha * detail_ ## NUM ## _normal); \
			}
		#else
			#define DETAIL_OVERLAP(DIFFUSE,NORMAL,NUM,SWIZZLE) { \
				float2 detail_texcoord = texcoord * detail_ ## NUM ## _transform.xy + detail_ ## NUM ## _transform.zw; \
				half4 detail_diffuse = s_texture_ ## DIFFUSE .Sample(s_sampler_ ## DIFFUSE,detail_texcoord); \
				half detail_alpha = saturate((detail_diffuse.w * mask. ## SWIZZLE - detail_ ## NUM ## _value + detail_ ## NUM ## _threshold * 0.5f) / max(detail_ ## NUM ## _threshold * 0.5f,1e-6f)); \
				diffuse.xyz = lerp(diffuse.xyz,detail_diffuse.xyz,detail_alpha * detail_ ## NUM ## _diffuse); \
			}
		#endif
		
		#ifdef DETAIL_0_OVERLAY
			DETAIL_OVERLAY(3,4,0,x);
		#elif DETAIL_0_OVERLAP
			DETAIL_OVERLAP(3,4,0,x);
		#endif
		#ifdef DETAIL_1_OVERLAY
			DETAIL_OVERLAY(5,6,1,y);
		#elif DETAIL_1_OVERLAP
			DETAIL_OVERLAP(5,6,1,y);
		#endif
		#ifdef DETAIL_2_OVERLAY
			DETAIL_OVERLAY(7,8,2,z);
		#elif DETAIL_2_OVERLAP
			DETAIL_OVERLAP(7,8,2,z);
		#endif
		#ifdef DETAIL_3_OVERLAY
			DETAIL_OVERLAY(9,10,3,w);
		#elif DETAIL_3_OVERLAP
			DETAIL_OVERLAP(9,10,3,w);
		#endif
		
		#undef DETAIL_OVERLAY
		#undef DETAIL_OVERLAP
		
		normal = normalize(basis_normal + basis_tangent * normal.x + basis_binormal * normal.y);
		
	#else
		
		half3 normal = texture2DNormal(s_texture_1,s_sampler_1,texcoord);
		
	#endif
	
	#ifdef USE_SRGB
		diffuse.xyz *= diffuse.xyz;
	#endif
	
	half3 specular = diffuse.xyz * diffuse.w;
	
	#ifdef ALPHA_TEST
		diffuse.w = saturate(alpha * 2.0f - 1.0f);
	#else
		diffuse.w = alpha;
	#endif
	
/******************************************************************************\
*
* Direct3D9
*
\******************************************************************************/

#elif DIRECT3D9
	
	half fade = IN.texcoord_0.z;
	
	float2 texcoord = IN.texcoord_0.xy / IN.texcoord_0.w;
	if(any(abs(texcoord.xy - 0.5f) > 0.5f)) discard;
	
	#ifdef BASE_DEFERRED
		half4 diffuse = tex2DAlphaDiscard(s_texture_0,texcoord,fade);
	#else
		half4 diffuse = tex2DAlpha(s_texture_0,texcoord,fade);
	#endif
	
	half alpha = diffuse.w;
	
	#ifdef DETAIL_0_OVERLAY || DETAIL_0_OVERLAP || DETAIL_1_OVERLAY || DETAIL_1_OVERLAP || DETAIL_2_OVERLAY || DETAIL_2_OVERLAP || DETAIL_3_OVERLAY || DETAIL_3_OVERLAP
		
		half3 normal = 0.0f;
		half3 basis_normal = tex2DNormal(s_texture_1,texcoord);
		half3 basis_binormal = normalize(cross(half3(-1.0f,0.0f,0.0f),basis_normal));
		half3 basis_tangent = normalize(cross(basis_binormal,basis_normal));
		
		half4 mask = tex2D(s_texture_2,texcoord);
		
		#ifdef QUALITY_MEDIUM
			#define DETAIL_OVERLAY(DIFFUSE,NORMAL,NUM,SWIZZLE) { \
				float2 detail_texcoord = texcoord * detail_ ## NUM ## _transform.xy + detail_ ## NUM ## _transform.zw; \
				half4 detail_diffuse = tex2D(s_texture_ ## DIFFUSE,detail_texcoord); \
				half3 detail_normal = tex2DNormal(s_texture_ ## NORMAL,detail_texcoord); \
				diffuse = saturate(diffuse + (detail_diffuse * 2.0f - 1.0f) * (detail_ ## NUM ## _diffuse * mask. SWIZZLE)); \
				normal = normal + detail_normal * (detail_ ## NUM ## _normal * mask. SWIZZLE); \
			}
		#else
			#define DETAIL_OVERLAY(DIFFUSE,NORMAL,NUM,SWIZZLE) { \
				float2 detail_texcoord = texcoord * detail_ ## NUM ## _transform.xy + detail_ ## NUM ## _transform.zw; \
				half4 detail_diffuse = tex2D(s_texture_ ## DIFFUSE,detail_texcoord); \
				diffuse = saturate(diffuse + (detail_diffuse * 2.0f - 1.0f) * (detail_ ## NUM ## _diffuse * mask. SWIZZLE)); \
			}
		#endif
		
		#ifdef QUALITY_MEDIUM
			#define DETAIL_OVERLAP(DIFFUSE,NORMAL,NUM,SWIZZLE) { \
				float2 detail_texcoord = texcoord * detail_ ## NUM ## _transform.xy + detail_ ## NUM ## _transform.zw; \
				half4 detail_diffuse = tex2D(s_texture_ ## DIFFUSE,detail_texcoord); \
				half3 detail_normal = tex2DNormal(s_texture_ ## NORMAL,detail_texcoord); \
				half detail_alpha = saturate((detail_diffuse.w * mask. ## SWIZZLE - detail_ ## NUM ## _value + detail_ ## NUM ## _threshold * 0.5f) / max(detail_ ## NUM ## _threshold * 0.5f,1e-6f)); \
				diffuse.xyz = lerp(diffuse.xyz,detail_diffuse.xyz,detail_alpha * detail_ ## NUM ## _diffuse); \
				normal.xy = lerp(normal.xy,detail_normal.xy,detail_alpha * detail_ ## NUM ## _normal); \
			}
		#else
			#define DETAIL_OVERLAP(DIFFUSE,NORMAL,NUM,SWIZZLE) { \
				float2 detail_texcoord = texcoord * detail_ ## NUM ## _transform.xy + detail_ ## NUM ## _transform.zw; \
				half4 detail_diffuse = tex2D(s_texture_ ## DIFFUSE,detail_texcoord); \
				half detail_alpha = saturate((detail_diffuse.w * mask. ## SWIZZLE - detail_ ## NUM ## _value + detail_ ## NUM ## _threshold * 0.5f) / max(detail_ ## NUM ## _threshold * 0.5f,1e-6f)); \
				diffuse.xyz = lerp(diffuse.xyz,detail_diffuse.xyz,detail_alpha * detail_ ## NUM ## _diffuse); \
			}
		#endif
		
		#ifdef DETAIL_0_OVERLAY
			DETAIL_OVERLAY(3,4,0,x);
		#elif DETAIL_0_OVERLAP
			DETAIL_OVERLAP(3,4,0,x);
		#endif
		#ifdef DETAIL_1_OVERLAY
			DETAIL_OVERLAY(5,6,1,y);
		#elif DETAIL_1_OVERLAP
			DETAIL_OVERLAP(5,6,1,y);
		#endif
		#ifdef DETAIL_2_OVERLAY
			DETAIL_OVERLAY(7,8,2,z);
		#elif DETAIL_2_OVERLAP
			DETAIL_OVERLAP(7,8,2,z);
		#endif
		#ifdef DETAIL_3_OVERLAY
			DETAIL_OVERLAY(9,10,3,w);
		#elif DETAIL_3_OVERLAP
			DETAIL_OVERLAP(9,10,3,w);
		#endif
		
		#undef DETAIL_OVERLAY
		#undef DETAIL_OVERLAP
		
		normal = normalize(basis_normal + basis_tangent * normal.x + basis_binormal * normal.y);
		
	#else
		
		half3 normal = tex2DNormal(s_texture_1,texcoord);
		
	#endif
	
	#ifdef USE_SRGB
		diffuse.xyz *= diffuse.xyz;
	#endif
	
	half3 specular = diffuse.xyz * diffuse.w;
	
	#ifdef ALPHA_TEST
		diffuse.w = saturate(alpha * 2.0f - 1.0f);
	#else
		diffuse.w = alpha;
	#endif
	
/******************************************************************************\
*
* PlayStation3
*
\******************************************************************************/

#elif PLAYSTATION3
	
	half fade = IN.texcoord_0.z;
	
	float2 texcoord = IN.texcoord_0.xy / IN.texcoord_0.w;
	if(any(abs(texcoord.xy - 0.5f) > 0.5f)) discard;
	
	#ifdef BASE_DEFERRED
		half4 diffuse = tex2DAlphaDiscard(s_texture_0,texcoord,fade);
	#else
		half4 diffuse = tex2DAlpha(s_texture_0,texcoord,fade);
	#endif
	
	half alpha = diffuse.w;
	
	#ifdef DETAIL_0_OVERLAY || DETAIL_0_OVERLAP || DETAIL_1_OVERLAY || DETAIL_1_OVERLAP || DETAIL_2_OVERLAY || DETAIL_2_OVERLAP || DETAIL_3_OVERLAY || DETAIL_3_OVERLAP
		
		half3 normal = 0.0f;
		half3 basis_normal = tex2DNormal(s_texture_1,texcoord);
		half3 basis_binormal = normalize(cross(half3(-1.0f,0.0f,0.0f),basis_normal));
		half3 basis_tangent = normalize(cross(basis_binormal,basis_normal));
		
		half4 mask = h4tex2D(s_texture_2,texcoord);
		
		#ifdef QUALITY_MEDIUM
			#define DETAIL_OVERLAY(DIFFUSE,NORMAL,NUM,SWIZZLE) { \
				float2 detail_texcoord = texcoord * detail_ ## NUM ## _transform.xy + detail_ ## NUM ## _transform.zw; \
				half4 detail_diffuse = h4tex2D(s_texture_ ## DIFFUSE,detail_texcoord); \
				half3 detail_normal = tex2DNormal(s_texture_ ## NORMAL,detail_texcoord); \
				diffuse = saturate(diffuse + (detail_diffuse * 2.0f - 1.0f) * (detail_ ## NUM ## _diffuse * mask. SWIZZLE)); \
				normal = normal + detail_normal * (detail_ ## NUM ## _normal * mask. SWIZZLE); \
			}
		#else
			#define DETAIL_OVERLAY(DIFFUSE,NORMAL,NUM,SWIZZLE) { \
				float2 detail_texcoord = texcoord * detail_ ## NUM ## _transform.xy + detail_ ## NUM ## _transform.zw; \
				half4 detail_diffuse = h4tex2D(s_texture_ ## DIFFUSE,detail_texcoord); \
				diffuse = saturate(diffuse + (detail_diffuse * 2.0f - 1.0f) * (detail_ ## NUM ## _diffuse * mask. SWIZZLE)); \
			}
		#endif
		
		#ifdef QUALITY_MEDIUM
			#define DETAIL_OVERLAP(DIFFUSE,NORMAL,NUM,SWIZZLE) { \
				float2 detail_texcoord = texcoord * detail_ ## NUM ## _transform.xy + detail_ ## NUM ## _transform.zw; \
				half4 detail_diffuse = h4tex2D(s_texture_ ## DIFFUSE,detail_texcoord); \
				half3 detail_normal = tex2DNormal(s_texture_ ## NORMAL,detail_texcoord); \
				half detail_alpha = saturate((detail_diffuse.w * mask. ## SWIZZLE - detail_ ## NUM ## _value + detail_ ## NUM ## _threshold * 0.5f) / max(detail_ ## NUM ## _threshold * 0.5f,1e-6f)); \
				diffuse.xyz = lerp(diffuse.xyz,detail_diffuse.xyz,detail_alpha * detail_ ## NUM ## _diffuse); \
				normal.xy = lerp(normal.xy,detail_normal.xy,detail_alpha * detail_ ## NUM ## _normal); \
			}
		#else
			#define DETAIL_OVERLAP(DIFFUSE,NORMAL,NUM,SWIZZLE) { \
				float2 detail_texcoord = texcoord * detail_ ## NUM ## _transform.xy + detail_ ## NUM ## _transform.zw; \
				half4 detail_diffuse = h4tex2D(s_texture_ ## DIFFUSE,detail_texcoord); \
				half detail_alpha = saturate((detail_diffuse.w * mask. ## SWIZZLE - detail_ ## NUM ## _value + detail_ ## NUM ## _threshold * 0.5f) / max(detail_ ## NUM ## _threshold * 0.5f,1e-6f)); \
				diffuse.xyz = lerp(diffuse.xyz,detail_diffuse.xyz,detail_alpha * detail_ ## NUM ## _diffuse); \
			}
		#endif
		
		#ifdef DETAIL_0_OVERLAY
			DETAIL_OVERLAY(3,4,0,x);
		#elif DETAIL_0_OVERLAP
			DETAIL_OVERLAP(3,4,0,x);
		#endif
		#ifdef DETAIL_1_OVERLAY
			DETAIL_OVERLAY(5,6,1,y);
		#elif DETAIL_1_OVERLAP
			DETAIL_OVERLAP(5,6,1,y);
		#endif
		#ifdef DETAIL_2_OVERLAY
			DETAIL_OVERLAY(7,8,2,z);
		#elif DETAIL_2_OVERLAP
			DETAIL_OVERLAP(7,8,2,z);
		#endif
		#ifdef DETAIL_3_OVERLAY
			DETAIL_OVERLAY(9,10,3,w);
		#elif DETAIL_3_OVERLAP
			DETAIL_OVERLAP(9,10,3,w);
		#endif
		
		#undef DETAIL_OVERLAY
		#undef DETAIL_OVERLAP
		
		normal = normalize(basis_normal + basis_tangent * normal.x + basis_binormal * normal.y);
		
	#else
		
		half3 normal = tex2DNormal(s_texture_1,texcoord);
		
	#endif
	
	#ifdef USE_SRGB
		diffuse.xyz *= diffuse.xyz;
	#endif
	
	half3 specular = diffuse.xyz * diffuse.w;
	
	#ifdef ALPHA_TEST
		diffuse.w = saturate(alpha * 2.0f - 1.0f);
	#else
		diffuse.w = alpha;
	#endif
	
#endif
